import 'package:honeypotflutter/modules/hire/models/hire_rent_model.dart';
import 'package:honeypotflutter/net/public_http.dart';
import 'package:honeypotflutter/publicBussine/view/bussine_modal.dart';
import '../models/hire_empty_model.dart';
import '../models/hire_param.dart';
import '../models/hire_rent_rent_model.dart';

class HireApi {
  /*
  * 空箱查询
  * */
  static Future<List<HireEmptyModel>> emptyList() async {
    try {
      List list = await MGHttp.post("api/services/app/DeliveryPackage/MyAllQueryEmptyCabinetAsync", params: {});
      List<HireEmptyModel> modelList = list.map((e) => HireEmptyModel.fromJson(e)).toList();
      return modelList;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }

  /*
  * 已租用格口
  * */
  static Future<List<HireRentModel>> rentList(int page) async {
    Map<String, dynamic> param = {"page": page};
    try {
      List list = await MGHttp.get("api/services/app/ContractCabinet/GetMyContractCabinetAsync", params: param);
      List<HireRentModel> modelList = list.map((e) => HireRentModel.fromJson(e)).toList();
      return modelList;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }

  /*
  * 价格详情
  * */
  static Future<HireRentRentModel> priceInfo(int? deviceId) async {
    if (deviceId == null) {
      return Future.error("无ID可以");
    }
    Map<String, int> params = {"deviceId": deviceId};
    try {
      Map<String, dynamic> result = await MGHttp.get("api/services/app/ContractCabinet/GetContractCabinetPriceAsync", params: params, progress: "数据加载中");
      HireRentRentModel model = HireRentRentModel.fromJson(result);
      return model;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }

  /*
  * 租赁
  * */
  static Future<bool> rent(HireParam param) async {
    try {
      Map<String, dynamic> params = {
        "cabinetNumberS11": param.cabinetNumberS11,
        "cabinetNumberS12": param.cabinetNumberS12,
        "cabinetNumberS13": param.cabinetNumberS13,
        "buyDays": param.buyDays,
        "totalFee": param.totalFee,
        "deviceId": param.deviceId,
      };
      await MGHttp.post("api/services/app/ContractCabinet/CreateContractCabinetAsync", params: params, progress: "提交中");
      return true;
    } catch (error) {
      MGModal.totalError(error);
      return Future.error(error);
    }
  }
}
